package com.itheima.nio.tomcat.handler;

import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * 接收客户端连接的
 */
public class AcceptHandler {

    /**
     * 定义这个返回值的主要作用就是为了提高我们代码的扩展性
     */
    public SocketChannel connSocketChannel(SelectionKey selectionKey) {

        try {

            ServerSocketChannel serverSocketChannel1 = (ServerSocketChannel)selectionKey.channel() ;
            SocketChannel socketChannel = serverSocketChannel1.accept();        // 接收客户端的连接
            socketChannel.configureBlocking(false) ;                            // 将其设置为非阻塞
            Selector selector = selectionKey.selector();                        // 获取选择器
            socketChannel.register(selector , SelectionKey.OP_READ) ;           // 把SocketChannel注册到选择器上

            return socketChannel ;

        }catch ( Exception e) {
            e.printStackTrace();
        }

        return null ;

    }

}
